#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include "unistd.h"
#include "log.hpp"
#include "protocol.hpp"

#define NUM 1024

namespace client
{

    using namespace std;

    class CalClient
    {
    public:
        CalClient(const string &serverip, const uint16_t &serverport)
            : _sock(-1), _serverip(serverip), _serverport(serverport)
        {
        }
        void initClient()
        {
            // 1.创建socket
            _sock = socket(AF_INET, SOCK_STREAM, 0);
            if (_sock < 0)
            {
                cout << "sock create error" << endl;
                exit(2);
            }

            // 2.tcp要不要bin？要！不需要显示bind，让OS随机指定port！
            // 3.要不要listen？不要
            // 4.要不要accept？不要
            // 5.要什么呢？要发起链接
        }
        void start()
        {
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(_serverport);
            server.sin_addr.s_addr = inet_addr(_serverip.c_str());

            if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
            {
                cerr << "socket connect error" << endl;
            }
            else
            {
                string line;
                string inbuffer;
                while (true)
                {
                    cout << "mycal>>> ";
                    std::getline(cin, line);       // 1+1
                    cout << line << endl;
                    Request req = ParseLine(line); // 1+1
                    string content;
                    req.serialize(&content);
                    string send_string = enLength(content);
                    send(_sock, send_string.c_str(), send_string.size(), 0); // bug?? 不管

                    string package, text;
                    // "context_len"\r\n"exitcode result"\r\n
                    while (!recvRequest(_sock, inbuffer, &package))
                    {
                        cout << "recvRequest error" << endl;
                    }
                    while (!deLength(package, &text))
                    {
                        cout << "deLength error" << endl;
                    }
                    // "exitcode result"
                    Response resp;
                    cout << text << endl;
                    resp.deserialize(text);
                    cout << "exitCode: " << resp.exitcode << endl;
                    cout << "result: " << resp.result << endl;
                }
            }
            // cout << "循环退出" << endl;
        }
        Request ParseLine(std::string &line)
        {
            // 简易版本的状态机
            int status = 0; // 0:操作符之前 1：碰到了操作符 2：操作符之后
            string left, right;
            char op;
            for (int i = 0; i < line.size();)
            {
                switch (status)
                {
                case 0:
                {
                    if (isdigit(line[i]))
                        left.push_back(line[i++]);

                    else
                        status = 1;
                }
                break;
                case 1:
                {
                    op = line[i++];
                    status = 2;
                }
                break;
                case 2:
                {
                    if (isdigit(line[i]))
                        right.push_back(line[i++]);
                }
                break;

                default:
                    break;
                }
            }
            cout << std::stoi(left) << " " << std::stoi(right) << " " << op << endl;
            return Request(std::stoi(left),std::stoi(right),op);
        }
        ~CalClient()
        {
            if (_sock >= 0)
                close(_sock);
        }

    private:
        int _sock;
        string _serverip;
        uint16_t _serverport;
    };
}